package cn.dansj.common.utils.transfer;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class GetXmlNodeValue {
    public static List<Map<String, String>> getMybatisXmlSql(File file) throws IOException {
        return parseSql(readFile(file));
    }

    public static List<Map<String, String>> getMybatisXmlSql(InputStream file) {
        return parseSql(readStream(file));
    }

    private static List<Map<String, String>> parseSql(String text) {
        Pattern pattern = Pattern.compile("<(select|update|insert)(.*?)>+(.*?)</(select|update|insert)>", Pattern.DOTALL);
        Pattern idPattern = Pattern.compile("id=\"(.*?)\"");
        List<Map<String, String>> result = new ArrayList<>();
        Matcher sqlMatcher = pattern.matcher(text);
        while (sqlMatcher.find()) {
            Map<String, String> ele = new HashMap<>();
            ele.put("name", sqlMatcher.group(1));
            Matcher idMatcher = idPattern.matcher(sqlMatcher.group(2));
            ele.put("id", idMatcher.find() ? idMatcher.group(1) : "");
            ele.put("text", sqlMatcher.group(3).trim());
            result.add(ele);
        }
        return result;
    }

    private static String readFile(File file) throws IOException {
        StringBuilder builder = new StringBuilder();
        try (BufferedReader br = Files.newBufferedReader(Paths.get(file.getAbsolutePath()))) {
            String line;
            while ((line = br.readLine()) != null) {
                builder.append(line).append("\n");
            }
        }
        return builder.toString();
    }

    private static String readStream(InputStream inputStream) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();//输出流
            byte[] bytes = new byte[1024];
            int len;
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);//将读到的字节写入输出流
            }
            inputStream.close();
            String content = outputStream.toString();
            outputStream.close();
            return content;
        } catch (IOException e) {
            return "";
        }
    }

    public static void main(String[] args) {
        String text = "<java>\n" +
                "javaCode;\n" +
                "</java>\n" +
                "\n" +
                "select sysdate c from dual;\n" +
                "select sysdate x from dual;\n";
        Pattern pattern = Pattern.compile("<java>+(.*?)</java>|(.*?);", Pattern.DOTALL);
        Matcher sqlMatcher = pattern.matcher(text);
        while (sqlMatcher.find()) {
            System.out.println("***************************************");
            System.out.println(sqlMatcher.groupCount());
            System.out.println(sqlMatcher.group().trim());
            System.out.println("***************************************");
        }
    }
}